﻿/**
 * Cursor
 *
 * @author		Allen Chou
 * @version		1.0.1 (last update: Feb 14 2009)
 * @link        http://cjcat.blogspot.com
 * @link		http://cjcat2266.deviantart.com
 */

package idv.cjcat.display {
	import flash.display.*;
	import flash.events.*;

	/**
	 * The <code>Cursor</code> class can be used as a symbol base class,
	 * whose instances have the default behavior of following the mouse cursor.
	 * <p>Used along with <code>Mouse.hide()</code>, the <code>Cursor</code> class can create a custom mouse cursor appearance.</p>
	 */
	public class Cursor extends MovieClip {
		
		private var _active:Boolean = true;
		
		public function Cursor() {
			mouseEnabled = false;
			mouseChildren = false;
			addEventListener(Event.ADDED_TO_STAGE, added);
			addEventListener(Event.REMOVED_FROM_STAGE, rel);
		}
		
		/**
		 * Whether the cursor follows the mouse cursor.
		 */
		public function get active():Boolean { return _active; }
		public function set active(value:Boolean):void {
			_active = value;
			if (stage != null) {
				if (!_active) rel();
				else ael();
			}
		}
		
		private function update(e:MouseEvent):void {
			if (!visible) visible = true;
			this.x = parent.mouseX;
			this.y = parent.mouseY;
			
			e.updateAfterEvent();
		}
		
		private function added(e:Event):void {
			if (_active) ael();
		}
		
		private function hide(e:Event):void {
			this.visible = false;
		}
		
		private function ael(e:Event = null):void {
			stage.addEventListener(MouseEvent.MOUSE_MOVE, update);
			stage.addEventListener(Event.MOUSE_LEAVE, hide);
		}
		
		private function rel(e:Event = null):void {
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, update);
			stage.removeEventListener(Event.MOUSE_LEAVE, hide);
		}
		
	}
}